package com.tec.security.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tec.security.entity.News;
import com.tec.security.service.NewsLogService;
import com.tec.security.service.NewsService;
import com.tec.security.utils.response.Result;
import com.tec.security.utils.other.LimitPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Api(tags = "文章管理")
@RestController
@RequestMapping("/News")
public class NewsController {
    private final NewsService newsService;
    private final NewsLogService newsLogService;
    @Autowired
    public NewsController(NewsService newsService, NewsLogService newsLogService) {
        this.newsService = newsService;
        this.newsLogService = newsLogService;
    }

    /**
     * @description 新闻上传接口
     * @author 陆林
     * @date 2023-11-9
     */
    @ApiOperation(value = "新闻上传")
    @PostMapping("/insert")
    public Result insert(News news) {
        if(newsService.insertNews(news)){
            newsLogService.newsInfo(StpUtil.getLoginId().toString(),"新建新闻","01");
            return Result.success("上传成功",null);
        }else{
            return Result.success("上传失败",null);
        }
    }

    /**
     * @description 分页新闻列表接口
     * @author 陆林
     * @date 2023-11-9
     */
    @ApiOperation(value = "分页新闻列表")
    @PostMapping("/listPages")
    public Result listAll(LimitPage limitPage) {
        return Result.success("请求成功",newsService.pageList(limitPage));
    }

    /**
     * @description 文章总数接口
     * @author 陆林
     * @date 2023-11-9
     */
    @ApiOperation(value = "文章总数")
    @PostMapping("/countNoAudit")
    public Result countNoAudit(){
        Map<String,Object> map=new HashMap<>();
        QueryWrapper<News> adminnewsQueryWrapper=new QueryWrapper<>();
        return Result.success("请求成功",map);
    }

    /**
     * @description 编辑者新闻列表接口
     * @author 陆林
     * @date 2023-11-9
     */
    @ApiOperation(value = "编辑者新闻列表")
    @PostMapping("/myPages")
    public Result myPages(LimitPage limitPage,String name) {
        Map<String ,Object> map=newsService.myNews(limitPage,name);
        return Result.success("请求成功",map);
    }

    /**
     * @description 新闻更新接口
     * @author 陆林
     * @date 2023-11-9
     */
    @ApiOperation(value = "新闻更新")
    @PostMapping("/update")
    public Result update(News news) {
        if(newsService.repost(news)){
            newsLogService.newsInfo(StpUtil.getLoginId().toString(),"更新新闻","02");
            return Result.success("更新成功",null);
        }else {
            return Result.failure("更新失败",null);
        }
    }

    /**
     * @description 删除文章接口
     * @author 陆林
     * @date 2023-11-9
     */
    @ApiOperation(value = "删除新闻")
    @PostMapping("/delete")
    public Result delete(int id) {
        boolean _temp =newsService.deletedNews(id);
        if (_temp) {
            newsLogService.newsInfo(StpUtil.getLoginId().toString(),"删除新闻","03");
            return Result.success("删除成功",null);
        } else {
            return Result.failure("删除失败",null);
        }
    }
}
